package com.xingkeduo.controller;

import com.xingkeduo.model.po.TStoreWorkPlan;
import com.xingkeduo.model.vo.StorePlanVo;
import com.xingkeduo.service.StorePlanService;
import com.xingkeduo.util.pojo.base.BaseController;
import com.xingkeduo.util.tool.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺排班时间
 * @Author: 中文输入法发挥不稳定的刘川
 * @Date: 19-3-11 下午9:19
 */
@RestController
@RequestMapping("/store/plan")
public class StorePlanController{

    @Autowired
    StorePlanService storePlanService;

    /**
     * @param workPlan
     * @return java.lang.String
     * @description 保存店铺班次时间信息(早班, 晚班, 全班)
     * @author 中文输入法发挥不稳定的刘川
     * @date 19-3-12 下午1:22
     */
    @PostMapping("/saveStorePlan")
    public ResponseMessage saveStorePlan(@RequestBody TStoreWorkPlan workPlan) {
        if(StringUtils.isEmpty(workPlan.getModifier())){
            return ResponseMessage.error("未获取到提交人", 500);
        }
        if (StringUtils.isEmpty(workPlan.getStoreId())) {
            return ResponseMessage.error("店铺信息不完整", 500);
        }
        if (StringUtils.isEmpty(workPlan.getAllDayShiftEndTime())
                || StringUtils.isEmpty(workPlan.getAllDayShiftStartTime())) {
            return ResponseMessage.error("保存失败，请先设置全班时间", 500);
        }
        if (StringUtils.isEmpty(workPlan.getMorningShiftEndTime())
                || StringUtils.isEmpty(workPlan.getMorningShiftStartTime())) {
            return ResponseMessage.error("保存失败，请先设置早班时间", 500);
        }
        if (StringUtils.isEmpty(workPlan.getNightShiftEndTime())
                || StringUtils.isEmpty(workPlan.getNightShiftStartTime())) {
            return ResponseMessage.error("保存失败，请先设置晚班时间", 500);
        }
        //验证时间设置是否符合逻辑(开始时间早于结束时间)
        String validateTimeResult=storePlanService.validateWorkTime(workPlan);
        if(!StringUtils.isEmpty(validateTimeResult)){
            return ResponseMessage.error(validateTimeResult, 500);
        }

        int res = storePlanService.saveOrUpdateStorePlan(workPlan);

        if (res > 0) {
            return ResponseMessage.ok("保存店铺班次时间信息成功");
        }
        return ResponseMessage.error("操作异常,参数并未有任何修改", 500);
    }
    
    /**
     * @description  根据店铺ID获取店铺班次
     * @Author 情绪极其不稳定的Shadow
     * @Param [storeId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping(value = "/getStorePlan")
    public ResponseMessage getStorePlan(Long storeId){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("店铺ID为空",500);
        }
        TStoreWorkPlan storeWorkPlan = storePlanService.getStorePlanByStoreId(storeId);
        if(storeWorkPlan == null){
            return ResponseMessage.ok("还未设置班次");
        }
        StorePlanVo vo = new StorePlanVo(storeWorkPlan);
        return ResponseMessage.ok(vo);
    }

    /**
     * @description  根据店铺ID获取店铺班次设置 时间间隔
     * @Author 情绪极其不稳定的Shadow
     * @Param [storeId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/

    @GetMapping("/getTimeInterval")
    public ResponseMessage getTimeInterval(Long storeId){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("店铺Id不能为空", 500);
        }
        List<String> timeInterval = storePlanService.getTimeInterval(storeId);
        if(CollectionUtils.isEmpty(timeInterval)){
            return ResponseMessage.error("获取店铺营业时间失败or店铺最大库存颗粒为0");
        }
        return ResponseMessage.ok(timeInterval);
    }
}
